$(function() {
	selectCityEntity();//初始化加载城市下拉框数据【面积】
	selectCityEntityPrice();//初始化加载城市下拉框数据【总价】
	selectCityEntityOverlapping();//初始化加载城市下拉框数据【面积总价段交叉分析】
	init_mjd("echarts1");//成交面积分析【面积段】
	init_zjd("echarts3");//成交面积分析【总价段】
	init_mjzjdjcfx("echarts2");//面积总价段交叉分析
	queryProjectTransactions();//经纪人业绩排行榜
	selectCityListValue();//初始化加载城市下拉框数据【面积总价段交叉分析】
	queryProjectTransactionsByCityId();//经纪人业绩排行榜【城市条件查询】
});
/**
 * 成交面积分析【面积段】
 */
function init_mjd(id) {
	var cityId = $("#selectCityId").val(); 
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/transactionListingsAreaInfo",
		data: {
			merchantId: merchantId,
			cityId: cityId
		},
		dataType: 'json',
		success: function(res) {
			var files_areaNum = new Array();
			var value = res;
			if(value!=""){
				var areaNums=value[0].areaNum.split(",");
				for(var i in areaNums){
				   files_areaNum[i]=parseFloat(areaNums[i]);
			    }
			}else{
				files_areaNum = [0,0,0,0,0,0,0,0];
			}
			
			option_mjd(id,files_areaNum);
		},
		error: function(res) {}
	});
	
	function option_mjd(id,areaNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
			title: {
				text: '',
				left: 'left'
			},
		    color: ['#3398DB'],
		    tooltip : {
		        trigger: 'axis',
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        }
		    },
		    grid: {
		    	top:'20px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
		    xAxis : [
		        {
		            type : 'category',
		            data : ['<60','60-80','80-90','90-100','100-110','110-120','120-144','≥144'],
		            axisTick: {
		                alignWithLabel: true
		            }
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value'
		        }
		    ],
		    series : [
		        {
		            name:'数量',
		            type:'bar',
		            barWidth: 20,
		            itemStyle: {
			            normal: {color: '#ffcc99'}
			        },
		            data:areaNum
		           //data:[10, 52, 200, 334, 390, 330, 220,120]
		        }
		    ]
		};
		myChart.setOption(option);
	}
}
/**
 * 成交面积分析【总价段】
 */
function init_zjd(id) {
	var cityId = $("#selectPriceByCityId").val(); 
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/transactionListingsSumPriceInfo",
		data: {
			merchantId: merchantId,
			cityId: cityId
		},
		dataType: 'json',
		success: function(res) {
			var files_areaNum = new Array();
			var value = res;
			if(value!=""){
				var areaNums=value[0].areaNum.split(",");
				for(var i in areaNums){
				   files_areaNum[i]=parseFloat(areaNums[i]);
			    }
			}else{
				files_areaNum = [0,0,0,0,0,0,0,0];
			}
			
			option_mjd(id,files_areaNum);
		},
		error: function(res) {}
	});
	
	function option_mjd(id,areaNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		option = {
			title: {
				text: '',
				left: 'left'
			},
		    color: ['#3398DB'],
		    tooltip : {
		        trigger: 'axis',
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        }
		    },
		    grid: {
		    	top:'20px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
		    xAxis : [
		        {
		            type : 'category',
		            data : ['50>', '50-60', '60-70', '70-90', '90-110', '110-130', '130-160', '≥160'],
		            axisTick: {
		                alignWithLabel: true
		            }
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value'
		        }
		    ],
		    series : [
		        {
		            name:'数量',
		            type:'bar',
		            barWidth: '60%',
		            data:areaNum
		            //data:[10, 52, 200, 334, 390, 330, 220]
		        }
		    ]
		};
		myChart.setOption(option);
	}
}
/**
 * 面积总价段交叉分析
 */
function init_mjzjdjcfx(id) {
	var cityId = $("#selectOverlappingByCityId").val(); 
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/transactionListingsOverlappingInfo",
		data: {
			merchantId: merchantId,
			cityId: cityId
		},
		dataType: 'json',
		success: function(res) {
			var files_areaNum = new Array();
			var value = res;
			if(value!=""){
				var areaNums=value[0].areaNum.split(",");
				for(var i in areaNums){
				   files_areaNum[0]=[0,0,parseFloat(areaNums[0])];
				   files_areaNum[1]=[1,1,parseFloat(areaNums[1])];
				   files_areaNum[2]=[2,2,parseFloat(areaNums[2])];
				   files_areaNum[3]=[3,3,parseFloat(areaNums[3])];
				   files_areaNum[4]=[4,4,parseFloat(areaNums[4])];
				   files_areaNum[5]=[5,5,parseFloat(areaNums[5])];
				   files_areaNum[6]=[6,6,parseFloat(areaNums[6])];
				   files_areaNum[7]=[7,7,parseFloat(areaNums[7])];
			    }
			}else{
				files_areaNum = [[0,0,0],[1,1,0],[2,2,0],[3,3,0],[4,4,0],[5,5,0],[6,6,0],[7,7,0]];
			}
			
			option_mjd(id,files_areaNum);
		},
		error: function(res) {}
	});
	
	function option_mjd(id,areaNum){
		var myChart = echarts.init(document.getElementById("" + id + ""));
		var hours = ['60>', '60-80', '80-90', '90-100', '100-110', '110-120', '120-144','≥144'];
		var days = ['<50万', '50-60万', '60-70万','70-90万', '90-110万', '110-130万', '130-160万', '≥160万'];
		var data = areaNum;
		data = data.map(function (item) {
		    return [item[1], item[0], item[2] || '-'];
		});
		option = {
			title: {
				text: '',
				left: 'left'
			},
		    tooltip: {
		        position: 'top'
		    },
		    animation: false,
		    grid: {
		    	top:'20px',
		        left: '0',
		        right: '0',
		        bottom: '0',
		        containLabel: true
		    },
		    xAxis: {
		        type: 'category',
		        data: hours,
		        splitArea: {
		            show: true
		        }
		    },
		    yAxis: {
		        type: 'category',
		        data: days,
		        splitArea: {
		            show: true
		        }
		    },
		    visualMap: {
		       	min: 0,
		        max: 10,
		        calculable: true,
		        orient: 'horizontal',
		        left: 'center',
		        bottom: '100%'
		    },
		    series: [{
		        name: '数量',
		        type: 'heatmap',
		        data: data,
		        label: {
		            normal: {
		                show: true
		            }
		        },
		        itemStyle: {
		            emphasis: {
		                shadowBlur: 10,
		                shadowColor: 'rgba(0, 0, 0, 0.5)'
		            }
		        }
		    }]
		};
		myChart.setOption(option);
	}
}

/**
 * 初始化加载城市下拉框数据[面积]
 */
function selectCityEntity(){
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityListInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var ddl = $("#selectCityId");
			//删除节点
            RemoveOption();
            //添加默认节点 
            ddl.append("<option value=''>请选择</option>");
            //转成Json对象
            var result = eval(res);
            //循环遍历 下拉框绑定
            $(result).each(function (i) {
                var opt = $("<option></option>").text(result[i].cityName).val(result[i].cityId);
                ddl.append(opt);

            });
		},
		error: function(res) {}
	});
}
function RemoveOption() {
    $("#selectCityId option").remove();
}

/**
 * 初始化加载城市下拉框数据[总价]
 */
function selectCityEntityPrice(){
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityListInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var ddl = $("#selectPriceByCityId");
			//删除节点
            RemoveOptionPrice();
            //添加默认节点 
            ddl.append("<option value=''>请选择</option>");
            //转成Json对象
            var result = eval(res);
            //循环遍历 下拉框绑定
            $(result).each(function (i) {
                var opt = $("<option></option>").text(result[i].cityName).val(result[i].cityId);
                ddl.append(opt);

            });
		},
		error: function(res) {}
	});
}
function RemoveOptionPrice() {
    $("#selectPriceByCityId option").remove();
}
/**
 * 初始化加载城市下拉框数据[总价]
 */
function selectCityEntityOverlapping(){
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/overlappingCityListInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var ddl = $("#selectOverlappingByCityId");
			//删除节点
            RemoveOptionOverlapping();
            //添加默认节点 
            ddl.append("<option value=''>请选择</option>");
            //转成Json对象
            var result = eval(res);
            //循环遍历 下拉框绑定
            $(result).each(function (i) {
                var opt = $("<option></option>").text(result[i].cityName).val(result[i].cityId);
                ddl.append(opt);

            });
		},
		error: function(res) {}
	});
}
function RemoveOptionOverlapping() {
    $("#selectOverlappingByCityId option").remove();
}
/**
 * 经纪人业绩排行榜
 */
function queryProjectTransactions() {
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/brokerRankingInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var html = "";
			html += '<thead><tr>';
			html += '<th>排名</th>';
			html += '<th>姓名</th>';
			html += '<th>认购套数</th>';
			html += '<th>认购金额(万元)</th>';
			html += '<th>签约套数</th>';
			html += '<th>签约金额(万元)</th>';
			html += '</tr></thead><tbody>';
			for(var i = 0; i < res.length; i++) {
				var resValue = res[i];
				if(i==1 || i==3){
					html += '<tr>';
				}else{
					html += '<tr>';
				}
				html += '<td>' + (i+1) + '</td>';
				html += '<td>' + resValue.userName + '</td>';
				html += '<td>' + resValue.subscribeNumber + '</td>';
				html += '<td>' + resValue.subscribeAmount + '</td>';
				html += '<td>' + resValue.signNumber + '</td>';
				html += '<td>' + resValue.signAmount + '</td>';
				html += '</tr>';
			}
			$("#agentList").html(html+"</tbody>");
		},
		error: function(res) {}
	});

}
/**
 * 初始化加载城市下拉框数据【面积总价段交叉分析】
 */
function selectCityListValue(){
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/cityListByMerchantIdInfo",
		data: {
			merchantId: merchantId
		},
		dataType: 'json',
		success: function(res) {
			var ddl = $("#cityValue");
			//删除节点
            RemoveOptionOverlappingCityValue();
            //添加默认节点 
            ddl.append("<option value=''>请选择</option>");
            //转成Json对象
            var result = eval(res);
            //循环遍历 下拉框绑定
            $(result).each(function (i) {
                var opt = $("<option></option>").text(result[i].cityName).val(result[i].cityId);
                ddl.append(opt);

            });
		},
		error: function(res) {}
	});
}
function RemoveOptionOverlappingCityValue() {
    $("#cityValue option").remove();
}

/**
 * 经纪人业绩排行榜【城市条件查询】
 */
function queryProjectTransactionsByCityId() {
	var cityId = $("#cityValue").val();
	$.ajax({
		type: 'post',
		url: projectConfit.apiUrl + "/reportApi/brokerRankingInfo",
		data: {
			merchantId: merchantId,
			cityId: cityId
		},
		dataType: 'json',
		success: function(res) {
			var html = "";
			html += '<thead><tr>';
			html += '<th>排名</th>';
			html += '<th>姓名</th>';
			html += '<th>认购套数</th>';
			html += '<th>认购金额(万元)</th>';
			html += '<th>签约套数</th>';
			html += '<th>签约金额(万元)</th>';
			html += '</tr><thead><tbody>';
			for(var i = 0; i < res.length; i++) {
				var resValue = res[i];
				if(i==1 || i==3){
					html += '<tr>';
				}else{
					html += '<tr>';
				}
				html += '<td>' + (i+1) + '</td>';
				html += '<td>' + resValue.userName + '</td>';
				html += '<td>' + resValue.subscribeNumber + '</td>';
				html += '<td>' + resValue.subscribeAmount + '</td>';
				html += '<td>' + resValue.signNumber + '</td>';
				html += '<td>' + resValue.signAmount + '</td>';
				html += '</tr>';
			}
			$("#cityAgentList").html(html+"</tbody>");
		},
		error: function(res) {}
	});

}